package com.kedll.library.util;

import android.content.Context;

import com.kedll.library.R;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author andytang
 * @created Jun 17, 2014 11:43:34 AM
 */
public class DateUtil {
    public static final TimeZone tz = TimeZone.getTimeZone("GMT+8:00");
    public static final int ONE_DAY_SECONDS = 24 * 3600;
    public static final int ONE_DAY_IN_MILLIS = ONE_DAY_SECONDS * 1000;
    public static final int SHOW_TYPE_SIMPLE = 0;
    public static final int SHOW_TYPE_COMPLEX = 1;
    public static final int SHOW_TYPE_ALL = 2;
    public static final int SHOW_TYPE_CALL_LOG = 3;
    public static final int SHOW_TYPE_CALL_DETAIL = 4;

    public static final SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy-MM-dd");

    public static String nowTimeInSimpleFormat() {
        return simpleFormat(System.currentTimeMillis());
    }

    public static String simpleFormat(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyy/MM/dd HH:mm:ss");
        return sdf.format(time);
    }

    public static long getCurrentDayTime() {
        Date d = new Date(System.currentTimeMillis());

        String formatDate = yearFormat.format(d);
        try {
            return (yearFormat.parse(formatDate)).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * @param milliSeconds
     * @return
     */
    public static String formatDuration(long milliSeconds) {
        //TODO
        NumberFormat formatter = new DecimalFormat("#0.0");
        return String.format("%s mins", formatter.format((double) milliSeconds / (60)));
    }

    /**
     * String转Date
     *
     * @param date
     * @return
     * @throws java.text.ParseException
     */
    public static Date stringToDate(String format, String date)
            throws ParseException {
//        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.parse(date);
    }

    /**
     * date转String
     *
     * @param format
     * @param date
     * @return
     */
    public static String dateToString(String format, Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
        return dateFormat.format(date);
    }

    public static final int ONE_MINUTE = 60;
    public static final int ONE_HOUR = 3600;
    public static final int ONE_DAY = 3600 * 24;


    public static Calendar ceilZeroTime(Calendar calendar) {
        Calendar c = zeroTime(calendar);
        c.add(Calendar.DAY_OF_MONTH, 1);
        return c;
    }

    /**
     * @param calendar
     * @return
     */
    public static Calendar zeroTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar;
    }

    /**
     * 取当天零点的值
     *
     * @param t
     * @return
     */
    public static long zeroTimeInMillis(long t) {
        Calendar d = Calendar.getInstance();
        d.setTimeInMillis(t);
        d.set(Calendar.HOUR_OF_DAY, 0);
        d.set(Calendar.MINUTE, 0);
        d.set(Calendar.SECOND, 0);
        d.set(Calendar.MILLISECOND, 0);
        return d.getTimeInMillis();
    }

    public static int zeroWeek(long t) {
        Calendar d = Calendar.getInstance();
        d.setTimeInMillis(t);
        return d.get(Calendar.WEEK_OF_YEAR);
    }


    public static int zeroMonth(long t) {
        Calendar d = Calendar.getInstance();
        d.setTimeInMillis(t);
        return d.get(Calendar.MONTH);
    }

    /**
     * 取下一天零点的值
     *
     * @param t
     * @return
     */
    public static long ceilZeroTimeInMillis(long t) {
        return zeroTimeInMillis(t) + ONE_DAY_IN_MILLIS;
    }

    /**
     * 两日期相差几天
     *
     * @param t1
     * @param t2
     * @return
     */
    public static int daysBetween(long t1, long t2) {
        return (int) ((zeroTimeInMillis(t2) - zeroTimeInMillis(t1)) / ONE_DAY_IN_MILLIS);
    }

    /**
     * 相差几周
     *
     * @param t1
     * @param t2
     * @return
     */
    public static int weekBetween(long t1, long t2) {
        return (zeroWeek(t1) - zeroWeek(t2));
    }

    /**
     * 相差几个月
     *
     * @param t1
     * @param t2
     * @return
     */
    public static int monthBetween(long t1, long t2) {
        return (zeroMonth(t1) - zeroMonth(t2));
    }

    /**
     * 整型方式表达的日期（只有年月日）
     *
     * @param t
     * @return
     */
    public static int dateInInt(long t) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        return Integer.valueOf(df.format(t));
    }

    public static long intToDate(int date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        try {
            return df.parse(String.valueOf(date)).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String dateFormat(long t) {
        return dateFormat("HH:mm", t);
    }

    public static String dateFormat(String format, long t) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(t);
    }

    public static String seconds2Text(Context ctx, long time) {
        String useText = "";
        int hour;
        int minute;
        if (time >= 60) {
            //分钟
            int ms = (int) (time / 60);
            if (ms >= 60) {
                hour = (ms / 60);
                minute = (ms % 60);
                useText = hour + ctx.getString(R.string.hour);
                useText += String.format(Locale.ROOT, "%02d%s", minute, ctx.getString(R.string.minute));
            } else {
                useText = ms + ctx.getString(R.string.minute);
                int second = (int) (time % 60);
                useText += String.format(Locale.ROOT, "%02d%s", second, ctx.getString(R.string.second));
            }
        } else {
            useText = time + ctx.getString(R.string.second);
        }
        return useText;
    }

    /**
     * 返回日期数组，
     *
     * @param t
     * @return
     */
    public static int[] dateInLong(long t) {
        int[] dates = new int[3];
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        dates[0] = Integer.valueOf(df.format(t));
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTimeInMillis(t);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        dates[1] = calendar.get(Calendar.WEEK_OF_MONTH);
        dates[2] = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (dates[2] == 0) {
            dates[1] -= 1;
            dates[2] = 7;
        }
        return dates;

    }

//    public static String getDateString(long time) {
//        Calendar c = Calendar.getInstance(tz);
//        c.setTimeInMillis(time);
//        long currentTime = System.currentTimeMillis();
//        Calendar current_c = Calendar.getInstance(tz);
//        current_c.setTimeInMillis(currentTime);
//
//        int currentYear = current_c.get(Calendar.YEAR);
//        int y = c.get(Calendar.YEAR);
//        int m = c.get(Calendar.MONTH) + 1;
//        int d = c.get(Calendar.DAY_OF_MONTH);
//        int hour = c.get(Calendar.HOUR_OF_DAY);
//        int minute = c.get(Calendar.MINUTE);
//        int second = c.get(Calendar.SECOND);
//        long t = currentTime - time;
//        long t2 = currentTime - getCurrentDayTime();
//        String dateStr = "";
//        if (t < t2 && t > 0) {//当日
//            dateStr = (hour < 10 ? "0" + hour : hour) + ":"
//                    + (minute < 10 ? "0" + minute : minute);
//        } else if (y == currentYear) {//当年
//            dateStr = (m < 10 ? "0" + m : m) + "月" + (d < 10 ? "0" + d : d) + "日";
//        } else {
//            dateStr = y + "年" + (m < 10 ? "0" + m : m) + "月"
//                    + (d < 10 ? "0" + d : d) + "日";
//        }
//        return dateStr;
//
//    }

    public static String getDateString(Context mContext, long time) {
        String dateStr = "";
        long today = getCurrentDayTime();
        if (time == today) {
            dateStr = mContext.getString(R.string.today);
        } else if (today - time == ONE_DAY_IN_MILLIS) {
            dateStr = mContext.getString(R.string.yesterday);
        } else if (time - today == ONE_DAY_IN_MILLIS) {
            dateStr = mContext.getString(R.string.tomorrow);
        } else {
            dateStr = dateFormat("yyyy-MM-dd E", time);
        }

        return dateStr;
    }

    public static String getDateString(Context mContext, long time, int type) {
        Calendar c = Calendar.getInstance(tz);
        c.setTimeInMillis(time);
        long currentTime = System.currentTimeMillis();
        Calendar current_c = Calendar.getInstance(tz);
        current_c.setTimeInMillis(currentTime);

        int currentYear = current_c.get(Calendar.YEAR);
        int y = c.get(Calendar.YEAR);
        int m = c.get(Calendar.MONTH) + 1;
        int d = c.get(Calendar.DAY_OF_MONTH);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);
        long t = currentTime - time;
        long t2 = currentTime - getCurrentDayTime();
        String dateStr = "";
        if (t <= t2 && t > 0) {
            if (type == SHOW_TYPE_SIMPLE) {
                dateStr = (hour < 10 ? "0" + hour : hour) + ":"
                        + (minute < 10 ? "0" + minute : minute);
            } else if (type == SHOW_TYPE_COMPLEX) {
                dateStr = mContext.getString(R.string.today) + (hour < 10 ? "0" + hour : hour) + ":"
                        + (minute < 10 ? "0" + minute : minute);
            } else if (type == SHOW_TYPE_CALL_LOG) {
                dateStr = mContext.getString(R.string.today) + (hour < 10 ? "0" + hour : hour) + ":"
                        + (minute < 10 ? "0" + minute : minute);
            } else if (type == SHOW_TYPE_CALL_DETAIL) {
                dateStr = mContext.getString(R.string.today);
            } else {
                dateStr = (hour < 10 ? "0" + hour : hour) + ":"
                        + (minute < 10 ? "0" + minute : minute) + ":"
                        + (second < 10 ? "0" + second : second);
            }
        } else if (t <= (t2 + ONE_DAY_IN_MILLIS) && t > 0) {
            if (type == SHOW_TYPE_SIMPLE || type == SHOW_TYPE_CALL_DETAIL) {
                dateStr = mContext.getString(R.string.yesterday);
            } else if (type == SHOW_TYPE_COMPLEX) {
                dateStr = mContext.getString(R.string.yesterday) + (hour < 10 ? "0" + hour : hour) + ":"
                        + (minute < 10 ? "0" + minute : minute);
            } else if (type == SHOW_TYPE_CALL_LOG) {
                dateStr = mContext.getString(R.string.yesterday) + (hour < 10 ? "0" + hour : hour) + ":"
                        + (minute < 10 ? "0" + minute : minute);
            } else {
                dateStr = mContext.getString(R.string.yesterday) + (hour < 10 ? "0" + hour : hour) + ":"
                        + (minute < 10 ? "0" + minute : minute) + ":"
                        + (second < 10 ? "0" + second : second);
            }
        } else if (y == currentYear) {
            if (type == SHOW_TYPE_SIMPLE) {
                dateStr = (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d);
            } else if (type == SHOW_TYPE_COMPLEX) {
                dateStr = (m < 10 ? "0" + m : m) + mContext.getString(R.string.string_month) + (d < 10 ? "0" + d : d)
                        + mContext.getString(R.string.string_day);
            } else if (type == SHOW_TYPE_CALL_LOG || type == SHOW_TYPE_COMPLEX) {
                dateStr = (m < 10 ? "0" + m : m) + /* 月 */"-"
                        + (d < 10 ? "0" + d : d) + /* 日 */" "
                        + (hour < 10 ? "0" + hour : hour) + ":"
                        + (minute < 10 ? "0" + minute : minute);
            } else if (type == SHOW_TYPE_CALL_DETAIL) {
                dateStr = y + "-" + (m < 10 ? "0" + m : m) + "-"
                        + (d < 10 ? "0" + d : d);
            } else {
                dateStr = (m < 10 ? "0" + m : m) + mContext.getString(R.string.string_month) + (d < 10 ? "0" + d : d)
                        + mContext.getString(R.string.string_day) + (hour < 10 ? " 0" + hour : hour) + ":"
                        + (minute < 10 ? "0" + minute : minute) + ":"
                        + (second < 10 ? "0" + second : second);
            }
        } else {
            if (type == SHOW_TYPE_SIMPLE) {
                dateStr = y + "-" + (m < 10 ? "0" + m : m) + "-"
                        + (d < 10 ? "0" + d : d);
            } else if (type == SHOW_TYPE_COMPLEX) {
                dateStr = y + mContext.getString(R.string.string_year) + (m < 10 ? "0" + m : m) + mContext.getString(R.string.string_month)
                        + (d < 10 ? "0" + d : d) + mContext.getString(R.string.string_day);
            } else if (type == SHOW_TYPE_CALL_LOG || type == SHOW_TYPE_COMPLEX) {
                dateStr = y + /* 年 */"-" + (m < 10 ? "0" + m : m) + /* 月 */"-"
                        + (d < 10 ? "0" + d : d) + /* 日 */"  "/*
                                                                 * + (hour < 10
																 * ? "0" + hour
																 * : hour) + ":"
																 * + (minute <
																 * 10 ? "0" +
																 * minute :
																 * minute)
																 */;
            } else if (type == SHOW_TYPE_CALL_DETAIL) {
                dateStr = y + "-" + (m < 10 ? "0" + m : m) + "-"
                        + (d < 10 ? "0" + d : d);
            } else {
                dateStr = y + mContext.getString(R.string.string_year) + (m < 10 ? "0" + m : m) + mContext.getString(R.string.string_month)
                        + (d < 10 ? "0" + d : d) + mContext.getString(R.string.string_day)
                        + (hour < 10 ? " 0" + hour : hour) + ":"
                        + (minute < 10 ? "0" + minute : minute) + ":"
                        + (second < 10 ? "0" + second : second);
            }
        }
        return dateStr;
    }

    public static long getCurrentTime() {
        return System.currentTimeMillis() / 1000;
    }

    public static String milSecondToString(long second) {
        String result = "%02d:%02d:%02d";
        return milSecondToString(result, second);
    }

    public static String milSecondToString(String format, long second) {
        int hour = (int) (second / 3600);
        int min = (int) ((second % 3600) / 60);
        int sec = (int) ((second % 3600) % 60);
        return String.format(format, hour, min, sec);
    }

    /**
     * �Ѻõ�ʱ����ʾ��ʽ
     *
     * @param used ʱ����s��
     * @return
     */
    public static String friendlyTime(long used) {
        used = (System.currentTimeMillis() - used) / 1000;
        if (used < ONE_MINUTE) {
            return "刚刚";
        } else if (used > ONE_MINUTE && used < ONE_HOUR) {
            return ((int) Math.ceil(used / ONE_MINUTE)) + "分钟前";
        } else if (used > ONE_HOUR && used < ONE_DAY) {
            return ((int) Math.ceil(used / ONE_HOUR)) + "小时前";
        } else if (used > ONE_DAY) {
            return ((int) Math.ceil(used / ONE_DAY)) + "天前";
        } else {
            return null;
        }
    }
}
